﻿using FlyingHat.Common.Global;
using FlyingHat.Model.Models.Global;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FlyingHat.Web.Controllers
{
    public class UploadController : BaseController
    {
        private readonly IWebHostEnvironment hostingEnvironment;
        public UploadController(IWebHostEnvironment hostingEnvironment)
        {
            this.hostingEnvironment = hostingEnvironment;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImage(IFormFile file, string? path = "default")
        {
            if (file == null)
            {
                return Json(ReturnModel.Fail("请选择图片"));
            }

            var filePath = string.Join(Path.DirectorySeparatorChar, new string[] { "Static", "Assets", "upload", path ?? "default", DateTime.Now.ToString("yyyyMMdd") });
            var absFilePath = hostingEnvironment.WebRootPath + Path.DirectorySeparatorChar + filePath;
            if (!Directory.Exists(absFilePath))
            {
                Directory.CreateDirectory(absFilePath);
            }
            try
            {
                var fileExtension = Path.GetExtension(file.FileName);
                if (fileExtension == null)
                {
                    return Json(ReturnModel.Fail("上传的图片没有后缀"));
                }
                if (GlobalConst.ImageExtension.IndexOf(fileExtension, StringComparison.OrdinalIgnoreCase) <= -1)
                {
                    return Json(ReturnModel.Fail("请上传jpg、png、gif格式的图片"));
                }

                long length = file.Length;
                if (length > GlobalConst.MaxLimit_5M) //5M
                {
                    return Json(ReturnModel.Fail("上传的图片不能大于5M"));
                }
                var saveName = Guid.NewGuid().ToString("N") + fileExtension;
                using FileStream fs = System.IO.File.Create(absFilePath + Path.DirectorySeparatorChar + saveName);
                await file.CopyToAsync(fs);
                await fs.FlushAsync();
                var filename = "/" + filePath.Replace("\\", "/") + "/" + saveName;
                return Json(ReturnModel.Success("上传成功", new { url = filename }));
            }
            catch
            {
                return Json(ReturnModel.Fail("上传的图片失败"));

            }
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadPdf(IFormFile file, string? path = "default")
        {
            if (file == null)
            {
                return Json(ReturnModel.Fail("请选择文件"));
            }

            var filePath = string.Join(Path.DirectorySeparatorChar, new string[] { "Static", "Assets", "upload", path ?? "default", DateTime.Now.ToString("yyyyMMdd") });
            var absFilePath = hostingEnvironment.WebRootPath + Path.DirectorySeparatorChar + filePath;
            if (!Directory.Exists(absFilePath))
            {
                Directory.CreateDirectory(absFilePath);
            }
            try
            {
                var fileExtension = Path.GetExtension(file.FileName);
                if (fileExtension == null)
                {
                    return Json(ReturnModel.Fail("上传的文件没有后缀"));
                }
                if (GlobalConst.PDFExtension.IndexOf(fileExtension, StringComparison.OrdinalIgnoreCase) <= -1)
                {
                    return Json(ReturnModel.Fail("请上传pdf格式的图片"));
                }

                long length = file.Length;
                if (length > GlobalConst.MaxLimit_500M) //500M
                {
                    return Json(ReturnModel.Fail("上传的图片不能大于500M"));
                }
                var saveName = Guid.NewGuid().ToString("N") + fileExtension;
                using FileStream fs = System.IO.File.Create(absFilePath + Path.DirectorySeparatorChar + saveName);
                await file.CopyToAsync(fs);
                await fs.FlushAsync();
                var filename = "/" + filePath.Replace("\\", "/") + "/" + saveName;
                return Json(ReturnModel.Success("上传成功", new { url = filename }));
            }
            catch
            {
                return Json(ReturnModel.Fail("上传的图片失败"));

            }
        }


    }
}
